<template>
  <!-- 表单 -->
  <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="focusFn" />
      <div v-if="showTree">
        <el-tree :data="treeData" :props="{ label:'name'}" default-expand-all @node-click="treeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelFn">取消</el-button>
      <el-button type="primary" @click="submitFn">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 导入枚举组件
import employees from '@/constant/employees'
// 导入获取部门列表
import { getDepartments } from '@/api/departments'
// 导入扁平数组装换树形结构
import { treeFn } from '@/utils/index'
// 导入添加员工函数
import { addEmployee } from '@/api/employees'

export default {
  name: 'EmpDialog',
  data() {
    return {
      // 要枚举的对象
      hireType: employees.hireType,
      // 添加表单字段
      // 字段和后端接口要求的字段要一致
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }, // 添加表单字段的校验
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [ // pattern 模式中写校验规则
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      treeData: [], // 树形结构,组织架构数据
      showTree: false // 是否展示树行组件
    }
  },
  methods: {
    // 重置表单
    resetFormFn() {
      // 1. 把表单的数据项清空
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      // 2.重置表单
      this.$refs.form.resetFields()
    },

    // 点击树形结构,获取到树形结构中该节点所对应的对象
    treeClick(data) {
      // 判断点击的部门,如果有子节点的话,不显示
      if (data && data.children) return
      // 重新给部门赋值
      this.formData.departmentName = data.name
      // 选择完毕后,隐藏树形结构
      this.showTree = false
    },
    // input获取焦点的时候,在发送请求,获取部门列表
    focusFn() {
      this.getDepartments()
    },
    // 获取部门列表
    async getDepartments() {
      const res = await getDepartments()
      this.treeData = treeFn(res.data.depts, '')
      this.showTree = true
    },
    // 添加确定添加按钮之后,进行兜底校验
    submitFn() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        const res = await addEmployee(this.formData)

        this.$message.success(res.message)

        this.$emit('update-empolyee')
      })
      // this.$emit('close')
    },
    // 取消添加
    cancelFn() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped></style>
